﻿using GMall.AdministrationService.Menus;
using System;
using System.Threading.Tasks;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Guids;

namespace GMall.AdministrationService.DataSeed
{
    public class MenuDataSeeder : ITransientDependency, IMenuDataSeeder
    {
        private readonly IGuidGenerator _guidGenerator;
        private readonly IRepository<Menu, Guid> _menuRepository;
        private readonly MenuManager _menuManager;
        public MenuDataSeeder(IGuidGenerator guidGenerator, IRepository<Menu, Guid> menuRepository, MenuManager menuManager)
        {
            _guidGenerator = guidGenerator;
            _menuRepository = menuRepository;
            _menuManager = menuManager;
        }
        public virtual async Task SeedAsync(Guid? tenantId = null)
        {
            if (await _menuRepository.GetCountAsync() > 0)
            {
                return;
            }
            var system_module = new Menu(_guidGenerator.Create(), "system_module", "系统管理", "", "", "", 0, MenuType.Module, true, true, null, null);
            await _menuManager.CreateAsync(system_module);
            await _menuRepository.InsertAsync(system_module, true);

            var menu_list = new Menu(_guidGenerator.Create(), "menu_list", "菜单列表", "/menu/list", "/api/administration/menus", "", 0, MenuType.Page, true, true, system_module.Id, null);
            await _menuManager.CreateAsync(menu_list);
            await _menuRepository.InsertAsync(menu_list, true);

            var menu_create = new Menu(_guidGenerator.Create(), "menu_create", "添加", "", "/api/administration/menus/create", "", 0, MenuType.Button, true, true, menu_list.Id, null);
            await _menuManager.CreateAsync(menu_create);
            await _menuRepository.InsertAsync(menu_create, true);

            var menu_delete = new Menu(_guidGenerator.Create(), "menu_delete", "删除", "", "/api/administration/menus/delete", "", 1, MenuType.Button, true, true, menu_list.Id, null);
            await _menuManager.CreateAsync(menu_delete);
            await _menuRepository.InsertAsync(menu_delete, true);

            var menu_update = new Menu(_guidGenerator.Create(), "menu_update", "修改", "", "/api/administration/menus/update", "", 2, MenuType.Button, true, true, menu_list.Id, null);
            await _menuManager.CreateAsync(menu_update);
            await _menuRepository.InsertAsync(menu_update, true);

            var menu_get = new Menu(_guidGenerator.Create(), "menu_get", "查看", "", "/api/administration/menus/get", "", 3, MenuType.Button, true, true, menu_list.Id, null);
            await _menuManager.CreateAsync(menu_get);
            await _menuRepository.InsertAsync(menu_get, true);
        }
    }
    public interface IMenuDataSeeder
    {
        Task SeedAsync(Guid? tenantId = null);
    }
    //public class MenuDataSeedContributor : IDataSeedContributor, ITransientDependency
    //{
    //    protected IMenuDataSeeder IMenuDataSeeder { get; }
    //    public MenuDataSeedContributor(IMenuDataSeeder iMenuDataSeeder)
    //    {
    //        IMenuDataSeeder = iMenuDataSeeder;
    //    }
    //    public virtual Task SeedAsync(DataSeedContext context)
    //    {
    //        return IMenuDataSeeder.SeedAsync(context?.TenantId);
    //    }
    //}
}
